/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.treess.web;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.treess.entity.YjyTree;
import com.jeeplus.modules.treess.service.YjyTreeService;

/**
 * fdController
 * @author 刘高峰
 * @version 2020-09-24
 */
@RestController
@RequestMapping(value = "/treess/yjyTree")
public class YjyTreeController extends BaseController {

	@Autowired
	private YjyTreeService yjyTreeService;

	@ModelAttribute
	public YjyTree get(@RequestParam(required=false) String id) {
		YjyTree entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = yjyTreeService.get(id);
		}
		if (entity == null){
			entity = new YjyTree();
		}
		return entity;
	}


	/**
	 * fd树表数据
	 */
	@RequiresPermissions("treess:yjyTree:list")
	@GetMapping("list")
	public AjaxJson list(YjyTree yjyTree) {
		return AjaxJson.success().put("list", yjyTreeService.findList(yjyTree));
	}

	/**
	 * 根据Id获取fd数据
	 */
	@RequiresPermissions(value={"treess:yjyTree:view","treess:yjyTree:add","treess:yjyTree:edit"},logical=Logical.OR)
	@GetMapping("queryById")
	public AjaxJson queryById(YjyTree yjyTree) {
		return AjaxJson.success().put("yjyTree", yjyTree);
	}

	/**
	 * 保存fd
	 */
	@RequiresPermissions(value={"treess:yjyTree:add","treess:yjyTree:edit"},logical=Logical.OR)
	@PostMapping("save")
	public AjaxJson save(YjyTree yjyTree, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(yjyTree);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		yjyTreeService.save(yjyTree);//保存
		return AjaxJson.success("保存fd成功");
	}

	/**
	 * 删除fd
	 */
	@RequiresPermissions("treess:yjyTree:del")
	@DeleteMapping("delete")
	public AjaxJson delete(YjyTree yjyTree) {
		yjyTreeService.delete(yjyTree);
		return AjaxJson.success("删除fd成功");
	}

	/**
	     * 获取JSON树形数据。
	     * @param extId 排除的ID
	     * @return
	*/
	@RequiresPermissions("user")
	@GetMapping("treeData")
	public AjaxJson treeData(@RequestParam(required = false) String extId) {
		List<YjyTree> list = yjyTreeService.findList(new YjyTree());
		List rootTree = getYjyTreeTree(list, extId);
		return AjaxJson.success().put("treeData", rootTree);
	}

	private List<YjyTree> getYjyTreeTree(List<YjyTree> list, String extId) {
		List<YjyTree> yjyTrees = Lists.newArrayList();
		List<YjyTree> rootTrees = yjyTreeService.getChildren("0");
		for (YjyTree root : rootTrees) {
		    if (StringUtils.isBlank(extId) ||  !extId.equals(root.getId())) {
		        yjyTrees.add(getChildOfTree(root, list, extId));
		    }
		}
		return yjyTrees;
	}

	private YjyTree getChildOfTree(YjyTree yjyTree, List<YjyTree> yjyTreeList, String extId) {
		yjyTree.setChildren(Lists.newArrayList());
		for (YjyTree child : yjyTreeList) {
		    if (StringUtils.isBlank(extId) ||  (!extId.equals(child.getId()) && child.getParentIds().indexOf("," + extId + ",") == -1)) {
		        if (child.getParentId().equals(yjyTree.getId())) {
		            yjyTree.getChildren().add(getChildOfTree(child, yjyTreeList, extId));
		        }
		    }
		}
		return yjyTree;
	}



}